package com.wanggsx.library.base.view;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wanggsx.library.util.UtilsAntiShake;
import com.wanggsx.library.util.UtilsToast;

/**
 * ```
 * author: wanggsx
 * blog  : http://blog.csdn.net/wanggsx918
 * time  : 2018/11/16
 * desc  : base about v4-fragment
 * ```
 */
public abstract class BaseFragment extends  Fragment implements View.OnClickListener{

    private static final String TAG = "BaseFragment";
    private static final String  STATE_SAVE_IS_HIDDEN = "STATE_SAVE_IS_HIDDEN";

    protected Activity mActivity;
    protected LayoutInflater mInflater;
    protected View mContentView;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (Activity) context;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState!=null){
            boolean  isSupportHidden = savedInstanceState.getBoolean(STATE_SAVE_IS_HIDDEN);
            FragmentTransaction trans = getFragmentManager().beginTransaction();
            if (isSupportHidden){
                trans.hide(this).commitAllowingStateLoss();
            }else{
                trans.show(this).commitAllowingStateLoss();
            }
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mInflater = inflater;
        setRootLayoutId();
        if (setRootLayoutId()>0){
            mContentView = mInflater.inflate(setRootLayoutId(), null);
        }else if (setRootLayoutId()==0){
            UtilsToast.showShort("请给页面 " + this.getClass().getSimpleName() +" 设置RootLayout!");
        }
        return mContentView;
    }

    public abstract int setRootLayoutId();

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData(savedInstanceState);
    }

    public abstract void initData(Bundle savedInstanceState);

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView(savedInstanceState,mContentView);
        doBusiness();
    }

    public abstract void initView(Bundle savedInstanceState,View rootView);

    public abstract void doBusiness();
    @Override
    public void onDestroyView() {
        ((ViewGroup)mContentView.getParent()).removeView(mContentView);
        super.onDestroyView();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean(STATE_SAVE_IS_HIDDEN,isHidden());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        if(UtilsAntiShake.isValid(v)){
            onWidgetClick(v);
        }
    }

    public abstract void onWidgetClick(View v);

    <T extends View> T findViewById(int id){
        return mContentView.findViewById(id);
    }
}
